package cn.ch.train.business.controller.admin;

import cn.ch.train.business.request.TrainQueryParam;
import cn.ch.train.business.request.TrainSaveParam;
import cn.ch.train.business.response.TrainQueryResult;
import cn.ch.train.business.service.TrainSeatService;
import cn.ch.train.business.service.TrainService;
import cn.ch.train.common.response.CommonResponse;
import cn.ch.train.common.response.PageResult;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/train")
public class TrainAdminController {
    @Autowired
    private TrainService trainService;
    @Autowired
    private TrainSeatService trainSeatService;

    @PostMapping("/save")
    public CommonResponse save(@Valid @RequestBody TrainSaveParam param) {
        trainService.save(param);
        return new CommonResponse<>();
    }

    @GetMapping("/query-list")
    public CommonResponse<PageResult<TrainQueryResult>> queryList(@Valid TrainQueryParam param) {
        PageResult<TrainQueryResult> trainList = trainService.queryList(param);
        return new CommonResponse<>(trainList);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResponse delete(@PathVariable Long id) {
        trainService.delete(id);
        return new CommonResponse<>();
    }

    @GetMapping("/query-all")
    public CommonResponse<List<TrainQueryResult>> queryAll() {
        List<TrainQueryResult> trainList = trainService.queryAll();
        return new CommonResponse<>(trainList);
    }

    @PostMapping("/gen-seat/{trainCode}")
    public CommonResponse genSeat(@PathVariable String trainCode) {
        trainSeatService.genTrainSeat(trainCode);
        return new CommonResponse<>();
    }

}
